#include<iostream>
#include<string>
using namespace std;
typedef long long LL;
const LL mod = 998244353;
int st1[30], st2[30];
string s, t;
LL n;
int main()
{
    cin >> n;
    cin >> s >> t;
    for (int i = 0; i < n; i++) {
        int k1 = s[i] - 'a' + 1;
        int k2 = t[i] - 'a' + 1;
        st1[k1]++;
        st2[k2]++;
    }
    bool flag = 0;
    for (int i = 1; i <= 26; i++) {
        if (st1[i] != st2[i]) {
            flag = 1;
            break;
        }
    }
    if (flag) {
        cout << -1 << endl;
        return 0;
    }
    int cnt = 0;
    int ans = n - 1;
    for (int i = n - 1; i >= 0; i--) {
        if (s[ans] != t[i]) {
            ans--;
            cnt++;
        }
    }
    cout << n - cnt << endl;
    return 0;
}
